package com.ruoyi.manage.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.manage.mapper.OrderMapper;
import com.ruoyi.manage.domain.Order;
import com.ruoyi.manage.service.IOrderService;

/**
 * 违法举报信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-12-30
 */
@Service
public class OrderServiceImpl implements IOrderService 
{
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderServiceImpl.class);
    @Autowired
    private WxSendMessageServiceImpl wxSendMessageService;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 查询违法举报信息
     * 
     * @param id 违法举报信息ID
     * @return 违法举报信息
     */
    @Override
    public Order selectOrderById(Long id)
    {
        return orderMapper.selectOrderById(id);
    }

    /**
     * 查询违法举报信息列表
     * 
     * @param order 违法举报信息
     * @return 违法举报信息
     */
    @Override
    public List<Order> selectOrderList(Order order)
    {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 新增违法举报信息
     * 
     * @param order 违法举报信息
     * @return 结果
     */
    @Override
    public int insertOrder(Order order)
    {
        order.setCreateTime(DateUtils.getNowDate());
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改违法举报信息
     * 
     * @param order 违法举报信息
     * @return 结果
     */
    @Override
    public int updateOrder(Order order)
    {
        //初始化参数
        Map<String,Object> sendParam = new HashMap<>();
        sendParam.put("dateTime",simpleDateFormat.format(new Date()));

        Order one = this.selectOrderById(order.getId());
        if(StringUtils.isEmpty(order.getDismissReason())){
            sendParam.put("dismissReason","无");
        }else{
            String dismissReason = order.getDismissReason();
            if(dismissReason.length()>15){
                dismissReason= dismissReason.substring(0, 15) + ".....";
            }
            sendParam.put("dismissReason",dismissReason);
        }
        Integer status = order.getStatus();
        if(status == 1){
            //通过
            sendParam.put("status","通过");
        }else if(status == 2){
            //驳回
            sendParam.put("status","驳回");
        }

        if(!one.getStatus().equals(order.getStatus())){
            //进行微信推送,获取推送模板和参数模板
            String templateId = DictUtils.getDictValue("wx_send_template_config", order.getStatus()+"", "");
            String message = DictUtils.getDictValue("wx_template_config", order.getStatus()+"", "");
            String page = DictUtils.getDictValue("wx_send_page_config", "page", "");

            if(StringUtils.isNotEmpty(templateId) && StringUtils.isNotEmpty(message)){
                String replaceMessage = message;
                for (Map.Entry<String, Object> stringObjectEntry : sendParam.entrySet()) {
                    String key = stringObjectEntry.getKey();
                    Object value = stringObjectEntry.getValue();
                    String repKey = "#{"+key+"}";
                    replaceMessage = replaceMessage.replace(repKey, String.valueOf(value));
                }
                Map map = JSONObject.parseObject(replaceMessage, Map.class);
                //进行推送
                boolean b = wxSendMessageService.sendWxMessage(one.getOpenId(), templateId, page, map);
                LOGGER.info("微信发送结果{}",b);
            }
        }
        order.setUpdateTime(DateUtils.getNowDate());
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除违法举报信息
     * 
     * @param ids 需要删除的违法举报信息ID
     * @return 结果
     */
    @Override
    public int deleteOrderByIds(Long[] ids)
    {
        return orderMapper.deleteOrderByIds(ids);
    }

    /**
     * 删除违法举报信息信息
     * 
     * @param id 违法举报信息ID
     * @return 结果
     */
    @Override
    public int deleteOrderById(Long id)
    {
        return orderMapper.deleteOrderById(id);
    }
}
